package com.javasm.adminapi.module.supplier.controller;

import com.github.pagehelper.PageInfo;

import com.javasm.adminapi.common.R;
import com.javasm.adminapi.module.product1.entity.GgGoods;
import com.javasm.adminapi.module.supplier.dto.GoodsAndSupplierDTO;
import com.javasm.adminapi.module.supplier.entity.GgSupplier;
import com.javasm.adminapi.module.supplier.entity.GgSupplierGoods;
import com.javasm.adminapi.module.supplier.service.GgSupplierGoodsService;
import com.javasm.adminapi.module.supplier.service.GgSupplierService;
import com.javasm.adminapi.module.supplier.vo.ListVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @className: SupplierController
 * @description:
 * @author: lrg
 * @date: 2025/10/14 20:38
 * @version: 0.1
 * @since: jdk17
 */

@RestController
@RequestMapping("/supplier")
public class SupplierController {


    @Resource
    GgSupplierService supplierService;
    @Resource
    GgSupplierGoodsService ggSupplierGoodsService;
    @GetMapping("/supplier/list")
    public R listSupplierByCondition(ListVo vo){
        PageInfo<GgSupplier> list = supplierService.listSupplierByCondition(vo);
        return R.ok(list);
    }

    @PostMapping("/addSupplier")
    public R addSupplier(@RequestBody GgSupplier supplier){
        supplier.setCreateTime(new Date());
        supplier.setUpdateTime(new Date());
        supplierService.save(supplier);
        return R.ok();
    }

    @PostMapping("/updateSupplier")
    public R updateSupplier(@RequestBody GgSupplier supplier){
        supplier.setUpdateTime(new Date());
        supplierService.saveOrUpdate(supplier);
        return R.ok();
    }

    @PostMapping("/del")
    public R delSupplierByIds(@RequestParam List<Integer> ids) {
        supplierService.removeBatchByIds(ids); // 添加分号
        return R.ok();
    }

    @GetMapping("/product/list")
    public R listProductBySupplierId(@RequestParam Integer supplierId){
        List<GgGoods> list =supplierService.getProductListBySupplierId(supplierId);
        return R.ok(list);
    }


    @PostMapping("/product/del")
    public R delProductWithSupplier(@RequestParam("sid") Integer sid,
                                    @RequestParam("gid") Integer gid){
        ggSupplierGoodsService.removeBySidAndGid(sid,gid);

        return R.ok();
    }

    @PostMapping("/addPros")
    public R addPros(@RequestBody GoodsAndSupplierDTO dto){
        List<GgSupplierGoods> ggSupplierGoods = dto.getGgSupplierGoods();
        ggSupplierGoodsService.saveBatch(ggSupplierGoods);
        return R.ok();
    }
}
